/**
 * ...
 * @author Krzysztof Rozalski
 */

package hapi.flash.view;

import flash.display.DisplayObjectContainer;
import flash.display.DisplayObject;
import hapi.event.Notifier;
import hapi.event.HapiDispatcher;

class GenericDisplayObjectView<T:DisplayObject> {
	
	var mc:T;
	public var onShowOrHide(getOnShowOrHide, null):Notifier<Bool>;
	var showOrHideDispatcher:HapiDispatcher<Bool>;
	public var isVisible(getIsVisible, null):Bool;

	public function new(mc:T) {
		this.mc = mc;
		showOrHideDispatcher = new HapiDispatcher();
	}
	
	public function hide():Void {
		mc.visible = false;
		showOrHideDispatcher.dispatchEvent(false);
	}
	
	public function show():Void {
		mc.visible = true;
		showOrHideDispatcher.dispatchEvent(true);
	}
	
	public function displayOn(cont:DisplayObjectContainer, ?x:Float = 0, ?y:Float = 0):Void {
		cont.addChild(mc);
		mc.x = x;
		mc.y = y;
	}
	
	public function toString():String {
		return Type.getClassName(Type.getClass(this)) + "<" + mc + ">";
	}
	
	function getOnShowOrHide():Notifier<Bool> {
		return showOrHideDispatcher;
	}
	
	function getIsVisible() {
		return mc.visible;
	}
	
	public function remove() {
		hide();
		if(mc.parent != null) mc.parent.removeChild(mc);
	}
}